<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="版本管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #top>
        <GiForm v-model="queryForm" :options="options" :columns="queryFormColumns" @search="search" @reset="reset"></GiForm>
      </template>
      <template #toolbar-left>
        <a-button v-permission="['vending:softwareVersion:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
      </template>
      <template #softwareName="{ record }">
        <a-link @click="onDetail(record)">
          <a-typography-paragraph
            class="link-text"
            :ellipsis="{
              rows: 1,
              showTooltip: true,
              css: true,
            }"
          >
            {{ record.softwareName }}
          </a-typography-paragraph>
        </a-link>
      </template>
      <template #softwareType="{ record }">
        <GiCellTag :value="record.softwareType" :dict="software_type" />
      </template>
      <template #status="{ record }">
        <GiCellStatus :status="record.status" />
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link
            v-permission="['vending:softwareVersion:delete']"
            status="danger"
            title="删除"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <SoftwareAddModal ref="SoftwareAddModalRef" @save-success="search" />
    <SoftwareDetailDrawer ref="SoftwareDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import SoftwareAddModal from './SoftwareAddModal.vue'
import SoftwareDetailDrawer from './SoftwareDetailDrawer.vue'
import {
  type SoftwareQuery,
  type SoftwareResp,
  deleteSoftware,
  listSoftware,
} from '@/apis/vending/software'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import { useDict } from '@/hooks/app'
import type { Columns, Options } from '@/components/GiForm'
import { DisEnableStatusList } from '@/constant/common'

defineOptions({ name: 'SoftwareVersion' })

const { software_type } = useDict('software_type')

const options: Options = reactive({
  form: { layout: 'inline' },
  grid: { cols: { xs: 1, sm: 1, md: 2, lg: 4, xl: 4, xxl: 4 } },
  fold: { enable: false, index: 3, defaultCollapsed: true },
})

const queryForm = reactive<SoftwareQuery>({
  softwareName: undefined,
  softwareType: undefined,
  status: undefined,
  sort: ['createTime,desc'],
})

const queryFormColumns: Columns = reactive([
  {
    type: 'input',
    field: 'softwareName',
    formItemProps: {
      hideLabel: false,
    },
    label: '软件名称',
    props: {
      placeholder: '请输入软件名称',
    },
  },
  {
    type: 'select',
    field: 'softwareType',
    options: software_type,
    formItemProps: {
      hideLabel: false,
    },
    label: '软件类型',
    props: {
      placeholder: '请选择软件类型',
    },
  },
  {
    type: 'select',
    field: 'status',
    label: '状态',
    options: DisEnableStatusList,
    formItemProps: {
      hideLabel: false,
    },
    props: {
      placeholder: '请选择状态',
    },
  },
])

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete,
} = useTable((page) => listSoftware({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '软件名称', dataIndex: 'softwareName', slotName: 'softwareName', align: 'center' },
  { title: '软件类型', dataIndex: 'softwareType', slotName: 'softwareType', align: 'center', width: 100 },
  { title: '版本名', dataIndex: 'versionName', slotName: 'versionName', align: 'center', width: 100 },
  { title: '版本号', dataIndex: 'versionCode', slotName: 'versionCode', align: 'center', width: 100 },
  { title: '下载地址', dataIndex: 'url', slotName: 'url', align: 'center', minWidth: 360, ellipsis: true, tooltip: true, show: false },
  { title: '状态', dataIndex: 'status', slotName: 'status', align: 'center', width: 80 },
  { title: '描述', dataIndex: 'description', slotName: 'description', align: 'center', show: false, ellipsis: true, tooltip: true },
  { title: '创建人', dataIndex: 'createUserString', slotName: 'createUserString', align: 'center', ellipsis: true, tooltip: true },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime', align: 'center', ellipsis: true, tooltip: true },
  { title: '修改人', dataIndex: 'updateUserString', slotName: 'updateUserString', align: 'center', show: false, ellipsis: true, tooltip: true },
  { title: '修改时间', dataIndex: 'updateTime', slotName: 'updateTime', align: 'center', show: false, ellipsis: true, tooltip: true },
  {
    title: '操作',
    slotName: 'action',
    minWidth: 150,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['vending:softwareVersion:update', 'vending:softwareVersion:delete']),
  },
]

// 重置
const reset = () => {
  queryForm.softwareName = undefined
  queryForm.softwareType = undefined
  queryForm.status = undefined
  search()
}

// 删除
const onDelete = (record: SoftwareResp) => {
  return handleDelete(() => deleteSoftware(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true,
  })
}

const SoftwareAddModalRef = ref<InstanceType<typeof SoftwareAddModal>>()
// 新增
const onAdd = () => {
  SoftwareAddModalRef.value?.onAdd()
}

const SoftwareDetailDrawerRef = ref<InstanceType<typeof SoftwareDetailDrawer>>()
// 详情
const onDetail = (record: SoftwareResp) => {
  SoftwareDetailDrawerRef.value?.onDetail(record.id)
}
</script>

<style lang="scss" scoped></style>
